/**
 * Created by dbwyxl on 2017/9/19.
 */
module.exports =function($cookies,$scope, $rootScope, $state, $stateParams, contractLibraryService){

    //获取页面传递的参数
    $scope.params = $state.params;
    //案件详情案件id
    $scope.id = $stateParams.id;

    //图片数组
    $scope.imgList = [];
    $scope.curImg = '';
    $scope.curImgIndex = 0;
    $scope.totalImg = 10;
    $scope.loading = true;

  //模板详情信息
  $scope.tempInfo = {}


  //获取模板图片数组
  $scope.getTempInfo = function (id) {
    var params = {'id':id};
    contractLibraryService.findTemplateInfo(params).then(function (response) {
      var data = response.data;
      if(data.code == 1) {
        $scope.loading = false;
        $scope.imgList = data.data;
        $scope.totalImg = data.data.length;
        $scope.curImg = $scope.imgList[$scope.curImgIndex];
      }
    }, function (response) {

    });
  }

  //获取模板详情
  $scope.findTemplateDetail = function (id) {
    var params = {'contractId':id};
    contractLibraryService.findTemplateDetail(params).then(function (response) {
      var data = response.data;
      if(data.code == 1) {
        $scope.tempInfo = data.data;
      }
    }, function (response) {

    });
  }



  //获取
  $scope.init = function () {
    $scope.getTempInfo($scope.id);
    $scope.findTemplateDetail($scope.id);
  }

  $scope.init();

  $scope.pageNex = function () {
    if($scope.curImgIndex < ($scope.totalImg-1)){
      $scope.curImgIndex++;
      $scope.curImg = $scope.imgList[$scope.curImgIndex];
    }
  }

  $scope.pagePre = function () {
    if($scope.curImgIndex > 0) {
      $scope.curImgIndex--;
      $scope.curImg = $scope.imgList[$scope.curImgIndex];
    }
  }



  $scope.cancel = function () {
    window.history.back();
  }

    //下载附件
    $scope.downloadFileProgress = function (file) {
        /*taskId
         taskAttachedId*/
        var params = {'contractId':file.id, 'fileName':file.contractName};
        contractLibraryService.tempDownload(params).then(function (response) {
            var fileData = new Blob([response.data]);
            var link = document.createElement('a');
            link.href = URL.createObjectURL(fileData);
            link.download = file.contractName;
            link.click();
            // window.URL.revokeObjectURL(link.href);
        });
        

    }

    //取消收藏
    $scope.delCollect = function (obj) {
        var params={'templateId':obj.id}
        contractLibraryService.deleteUserTemplateCollect(params).then(function (response) {
            var data = response.data;
            if(data.code == 1) {
                $scope.tempInfo.isCollection = '0';
            }
        }, function (response) {
            console.log('fail');
        });
    }

    //添加收藏
    $scope.addCollect = function (obj) {
        var params={'templateId':obj.id, 'typeName': obj.typeName, 'parentName': obj.parentName}
        contractLibraryService.insertUserTemplateCollect(params).then(function (response) {
            var data = response.data;
            if(data.code == 1) {
                $scope.tempInfo.isCollection = '1';
            }
        }, function (response) {
            console.log('fail');
        });
    }


};
